package com.neusoft.neuiotms.dcom.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.HandoverDTO;
import com.neusoft.neuiotms.dcom.model.FunctionalGroup;
import com.neusoft.neuiotms.dcom.model.Handover;
import com.neusoft.neuiotms.dcom.model.HandoverItemValue;
import com.neusoft.neuiotms.dcom.repository.FunctionalGroupRepository;
import com.neusoft.neuiotms.dcom.repository.HandoverItemValueRepository;
import com.neusoft.neuiotms.dcom.repository.HandoverRepository;

@Component
public class HandoverConverter extends AbstractConverter<Handover, HandoverDTO> {

    @Autowired
    private HandoverItemValueConverter handoverItemValueConverter;
    @Autowired
    private HandoverRepository handoverRepository;
    @Autowired
    private HandoverItemValueRepository handoverItemValueRepository;
    @Autowired
    private FunctionalGroupRepository functionalGroupRepository;

    @Override
    public Handover toModel(final HandoverDTO dto) {

        Handover model;
        if (dto.isNew()) {
            model = new Handover();
        } else {
            model = this.handoverRepository.getOne(dto.getId());
        }

        model.setOrgCode(dto.getOrgCode());
        model.setOrgName(dto.getOrgName());
        model.setUpUserCode(dto.getUpUserCode());
        model.setUpUserName(dto.getUpUserName());
        model.setUpTime(dto.getUpTime());
        model.setUpNote(dto.getUpNote());
        model.setDownUserCode(dto.getDownUserCode());
        model.setDownUserName(dto.getDownUserName());
        model.setDownTime(dto.getDownTime());
        model.setDownNote(dto.getDownNote());
        model.setStatus(dto.getStatus());

        final List<HandoverItemValue> items = model.getItems().stream().collect(Collectors.toList());

        items.forEach(k -> {
            model.getItems().remove(k);
            this.handoverItemValueRepository.delete(k);
        });

        dto.getItems().stream().map(s -> {
            final HandoverItemValue handover = this.handoverItemValueConverter.toModel(s);
            handover.setHandover(model);
            return handover;
        }).forEach(model.getItems()::add);

        if (null != dto.getFunctionalGroupId()) {
            final FunctionalGroup functional = this.functionalGroupRepository.getOne(dto.getFunctionalGroupId());
            model.setFunctionalGroup(functional);
        }
        return model;
    }

    @Override
    public HandoverDTO toDTO(final Handover model, final boolean forListView) {

        final HandoverDTO dto = new HandoverDTO();

        dto.setId(model.getId());
        dto.setOrgCode(model.getOrgCode());
        dto.setOrgName(model.getOrgName());
        dto.setUpUserCode(model.getUpUserCode());
        dto.setUpUserName(model.getUpUserName());
        dto.setUpTime(model.getUpTime());
        dto.setUpNote(model.getUpNote());
        dto.setDownUserCode(model.getDownUserCode());
        dto.setDownUserName(model.getDownUserName());
        dto.setDownTime(model.getDownTime());
        dto.setDownNote(model.getDownNote());
        dto.setStatus(model.getStatus());

        if (!forListView) {
            model.getItems().stream().map(s -> this.handoverItemValueConverter.toDTO(s)).forEach(dto.getItems()::add);
        }

        if (null != model.getFunctionalGroup()) {
            dto.setFunctionalGroupId(model.getFunctionalGroup().getId());
            dto.setFunctionalGroupName(model.getFunctionalGroup().getName());
        }
        return dto;
    }

}
